package com.ibm.law.ui.system.setup;

import java.util.Iterator;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.beans.IBeanValueProperty;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.viewers.CellEditorProperties;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.ibm.law.data.CachedData;
import com.ibm.law.domain.Category;
import com.ibm.law.domain.CategoryList;
import com.ibm.law.domain.Document;
import com.ibm.law.service.system.CategoryService;

public class CategorySetup extends Composite {
	private DataBindingContext m_bindingContext;
	private CategoryList categories = new CategoryList();
	private Table table;
	private Table table_1;
	private TableViewer tableViewer;
	private TableViewerColumn tableViewerColumn;
	private TableViewerColumn tableViewerColumn_1;
	CategoryService categoryService = new CategoryService();
	private CheckboxTableViewer tableViewer_1;
	private List<Document> documents=CachedData.DOCUMENTS;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public CategorySetup(Composite parent, int style) {

		super(parent, style);
		categories.setList(categoryService.getAll());
		setLayout(new GridLayout(1, false));

		SashForm sashForm = new SashForm(this, SWT.NONE);
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));

		tableViewer = new TableViewer(sashForm, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);

		ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
				tableViewer) {
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
						|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
						|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
			}
		};

		TableViewerEditor.create(tableViewer, actSupport,
				ColumnViewerEditor.TABBING_HORIZONTAL
						| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
						| ColumnViewerEditor.TABBING_VERTICAL
						| ColumnViewerEditor.KEYBOARD_ACTIVATION);

		tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("鉴定类别");

		tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("缩略语");

		tableViewer_1 = CheckboxTableViewer.newCheckList(sashForm, SWT.BORDER
				| SWT.FULL_SELECTION | SWT.MULTI);
		table_1 = tableViewer_1.getTable();
		table_1.setHeaderVisible(true);

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(
				tableViewer_1, SWT.NONE);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_2.setWidth(169);
		tblclmnNewColumn_2.setText("文书类别");

		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(
				tableViewer_1, SWT.NONE);
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("缩略语");
		sashForm.setWeights(new int[] { 1, 1 });

		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Category c = new Category();
				categories.addCategory(c);
				// c.setId((long) (categories.getList().size()+1));
				System.out.println(categories.getList().size());
				System.out.println(tableViewer.getElementAt(categories
						.getList().size() - 1));
				tableViewerColumn.getViewer().editElement(
						categories.getList().get(
								categories.getList().size() - 1), 0);
			}
		});
		btnNewButton.setText("增加");

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection s = (StructuredSelection) tableViewer
						.getSelection();
				Iterator iterator = s.iterator();
				while (iterator.hasNext()) {
					Category c = (Category) iterator.next();
					categories.removeCategory(c);
					categoryService.delete(c);
				}
			}
		});
		btnNewButton_1.setText("删除");

		Button btnNewButton_2 = new Button(composite, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				CategoryService categoryService=new CategoryService();
				for(Category c:categories.getList()){
					System.out.println(c);
					
					for(Document d:c.getDocuments()){
//						d.setCategory(c);
						System.out.println(d);
					}
					categoryService.update(c);
				}
			}
		});
		btnNewButton_2.setText("保存");

		Button btnNewButton_3 = new Button(composite, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (e.getSource() instanceof Button) {
					Button button = (Button) e.getSource();
					button.getShell().close();
					
				}
			}
		});
		btnNewButton_3.setText("退出");
		m_bindingContext = initDataBindings();

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider.getKnownElements(), Category.class, new String[]{"name", "shortName"});
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		tableViewer.setContentProvider(listContentProvider);
		//
		IObservableList listCategoriesObserveList = BeanProperties.list("list").observe(categories);
		tableViewer.setInput(listCategoriesObserveList);
		//
		CellEditor cellEditor = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty = BeanProperties.value("name");
		tableViewerColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor, cellEditorProperty, valueProperty));
		//
		CellEditor cellEditor_1 = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty_1 = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty_1 = BeanProperties.value("shortName");
		tableViewerColumn_1.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor_1, cellEditorProperty_1, valueProperty_1));
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		IObservableMap[] observeMaps_1 = BeansObservables.observeMaps(listContentProvider_1.getKnownElements(), Document.class, new String[]{"name", "shortName"});
		tableViewer_1.setLabelProvider(new ObservableMapLabelProvider(observeMaps_1));
		tableViewer_1.setContentProvider(listContentProvider_1);
		//
		IObservableList selfList = Properties.selfList(Document.class).observe(documents);
		tableViewer_1.setInput(selfList);
		//
		final IObservableValue selectedPerson = ViewersObservables
				.observeSingleSelection(tableViewer);
		bindingContext.bindSet(ViewersObservables.observeCheckedElements(
				tableViewer_1, Document.class), BeansObservables
				.observeDetailSet(selectedPerson, "documents", Document.class));
		return bindingContext;
	}
}
